package com.liqi.sms.utils;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信发送
 */
public class MessageSendBusiness {

    private Logger logger = LoggerFactory.getLogger(MessageSendBusiness.class);


    public static MessageSendBusiness getInstance(){
        return new MessageSendBusiness();
    }

    public String shanWeiSendPost(String apiMethod,String reqContent,PlatformConfig platformConfig) throws Exception{
        long reqStartTime = System.currentTimeMillis();
        logger.info("请求地址:[{}]", platformConfig.getCenterRoot());
        logger.info("请求方法:[{}]", apiMethod);
        logger.info("请求参数：{}", reqContent);
        String  doSend = doPostSend(apiMethod, reqContent, platformConfig);
        logger.info("发送请求接口响应结果：{}", doSend);
        JSONObject messageSendResponse = JSONObject.parseObject(doSend);
        logger.info("发送请求结束，总耗时长：[{}]ms", System.currentTimeMillis()-reqStartTime);
        return messageSendResponse.toJSONString();
    }

    private String doPostSend(String apiMethod, String reqContent, PlatformConfig platformConfig) throws Exception{
        try {
            String fullUrl = String.format("%s%s", platformConfig.getCenterRoot(), apiMethod);
            Map<String, String> heardMap = new HashMap<>();
            heardMap.put("Content-Type", "application/json");
            String timestamp = AuthenticationUtils.getCurrentTimestamp();
            //设置签名
            heardMap.put("Authorization", TokenUtil.getToken(platformConfig.getCenterRoot()+ "auth/token", platformConfig.getUsername(), platformConfig.getPassword()));
            return SmartHttp.sendPostJson(fullUrl,reqContent,heardMap);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

}
